/**
 * @author tian qingsen
 */
$(function() {
	// 宣传图片一
	var uploadBtn1 = $('#uploadBtn1'), interval1;
	new AjaxUpload(uploadBtn1, {
		action : window.ctx + '/admin/upload/adv1_process.htm',
		name : 'fileInput',
		filetype : 'image/*',
		onChange: function(file, ext) {
			if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
				// 扩展名不允许
				$('#up_bk_msg1').text('不支持的图片类型，请重新选择');
				return false;
			}
		},
		onSubmit : function(file, ext) {
			$('#up_bk_msg1').text('上传中');
			uploadBtn1.prop('disabled', true);
	
			// 上传中 -> 上传中. -> 上传中...
			interval1 = window.setInterval(function() {
				var text = $('#up_bk_msg1').text();
				if (text.length < 6) {
					$('#up_bk_msg1').text(text + '.')
				} else {
					$('#up_bk_msg1').text('上传中')
				}
			}, 200);
		},
		onComplete : function(file, response) {
			if (BooleanHelper.isTrue(response.successful)) {
				setImgPath('adv_img1', response.message);
				$('#up_bk_msg1').text('');
			} else {
				$('#up_bk_msg1').text(response.message);
			}
			uploadBtn1.prop('disabled', false);
			window.clearInterval(interval1);
		}
	});
	
	// 宣传图片二
	var uploadBtn2 = $('#uploadBtn2'), interval2;
	new AjaxUpload(uploadBtn2, {
		action : window.ctx + '/admin/upload/adv2_process.htm',
		name : 'fileInput',
		filetype : 'image/*',
		onChange: function(file, ext) {
			if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
				// 扩展名不允许
				$('#up_bk_msg2').text('不支持的图片类型，请重新选择');
				return false;
			}
		},
		onSubmit : function(file, ext) {
			$('#up_bk_msg2').text('上传中');
			uploadBtn2.prop('disabled', true);
	
			// 上传中 -> 上传中. -> 上传中...
			interval2 = window.setInterval(function() {
				var text = $('#up_bk_msg2').text();
				if (text.length < 6) {
					$('#up_bk_msg2').text(text + '.')
				} else {
					$('#up_bk_msg2').text('上传中')
				}
			}, 200);
		},
		onComplete : function(file, response) {
			if (BooleanHelper.isTrue(response.successful)) {
				setImgPath('adv_img2', response.message);
				$('#up_bk_msg2').text('');
			} else {
				$('#up_bk_msg2').text(response.message);
			}
			uploadBtn2.prop('disabled', false);
			window.clearInterval(interval2);
		}
	});
	
	// 宣传图片三
	var uploadBtn3 = $('#uploadBtn3'), interval3;
	new AjaxUpload(uploadBtn3, {
		action : window.ctx + '/admin/upload/adv3_process.htm',
		name : 'fileInput',
		filetype : 'image/*',
		onChange: function(file, ext) {
			if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
				// 扩展名不允许
				$('#up_bk_msg3').text('不支持的图片类型，请重新选择');
				return false;
			}
		},
		onSubmit : function(file, ext) {
			$('#up_bk_msg3').text('上传中');
			uploadBtn3.prop('disabled', true);
	
			// 上传中 -> 上传中. -> 上传中...
			interval3 = window.setInterval(function() {
				var text = $('#up_bk_msg3').text();
				if (text.length < 6) {
					$('#up_bk_msg3').text(text + '.')
				} else {
					$('#up_bk_msg3').text('上传中')
				}
			}, 200);
		},
		onComplete : function(file, response) {
			if (BooleanHelper.isTrue(response.successful)) {
				setImgPath('adv_img3', response.message);
				$('#up_bk_msg3').text('');
			} else {
				$('#up_bk_msg3').text(response.message);
			}
			uploadBtn3.prop('disabled', false);
			window.clearInterval(interval3);
		}
	});
	
	// 宣传图片四
	var uploadBtn4 = $('#uploadBtn4'), interval4;
	new AjaxUpload(uploadBtn4, {
		action : window.ctx + '/admin/upload/adv4_process.htm',
		name : 'fileInput',
		filetype : 'image/*',
		onChange: function(file, ext) {
			if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
				// 扩展名不允许
				$('#up_bk_msg4').text('不支持的图片类型，请重新选择');
				return false;
			}
		},
		onSubmit : function(file, ext) {
			$('#up_bk_msg4').text('上传中');
			uploadBtn4.prop('disabled', true);
	
			// 上传中 -> 上传中. -> 上传中...
			interval4 = window.setInterval(function() {
				var text = $('#up_bk_msg4').text();
				if (text.length < 6) {
					$('#up_bk_msg4').text(text + '.')
				} else {
					$('#up_bk_msg4').text('上传中')
				}
			}, 200);
		},
		onComplete : function(file, response) {
			if (BooleanHelper.isTrue(response.successful)) {
				setImgPath('adv_img4', response.message);
				$('#up_bk_msg4').text('');
			} else {
				$('#up_bk_msg4').text(response.message);
			}
			uploadBtn4.prop('disabled', false);
			window.clearInterval(interval4);
		}
	});
	
	// 宣传图片五
	var uploadBtn5 = $('#uploadBtn5'), interval5;
	new AjaxUpload(uploadBtn5, {
		action : window.ctx + '/admin/upload/adv5_process.htm',
		name : 'fileInput',
		filetype : 'image/*',
		onChange: function(file, ext) {
			if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
				// 扩展名不允许
				$('#up_bk_msg5').text('不支持的图片类型，请重新选择');
				return false;
			}
		},
		onSubmit : function(file, ext) {
			$('#up_bk_msg5').text('上传中');
			uploadBtn5.prop('disabled', true);
	
			// 上传中 -> 上传中. -> 上传中...
			interval5 = window.setInterval(function() {
				var text = $('#up_bk_msg5').text();
				if (text.length < 6) {
					$('#up_bk_msg5').text(text + '.')
				} else {
					$('#up_bk_msg5').text('上传中')
				}
			}, 200);
		},
		onComplete : function(file, response) {
			if (BooleanHelper.isTrue(response.successful)) {
				setImgPath('adv_img5', response.message);
				$('#up_bk_msg5').text('');
			} else {
				$('#up_bk_msg5').text(response.message);
			}
			uploadBtn5.prop('disabled', false);
			window.clearInterval(interval5);
		}
	});
});

//上传图片成功后设置图片路劲
function setImgPath(imgId, path) {
	var imgsrc = window.ctx + path + '?'+ Math.floor(Math.random() * 100);
	$('#' + imgId).attr('src', imgsrc);
}
